package com.example.spingsercurity2.config;

import com.example.spingsercurity2.filter.JwtAuthenticationTokenFilter;
import com.example.spingsercurity2.util.MyAuthExcetion;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


/**
 * 核心配置
 *
 * configureAuthentication() 配置身份验证信息
 * passwordEncoder() 配置密码的加密使用的算法
 * authenticationTokenFilterBean() 构建自定义jwt权限认证过滤器
 * configure() 配置资源过滤和注入自定义jwt过滤器
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private MyAuthExcetion.MyAuthenticationEntry myAuthenticationEntry;

	@Autowired
	private MyAuthExcetion.MyAccessDenied myAccessDenied;

	@Autowired
	@Lazy
	private UserDetailsService userDetailsService;


	@Bean
	@Lazy
	public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
		authenticationManagerBuilder
			// 设置 UserDetailsService
			.userDetailsService(userDetailsService)
			// 使用 BCrypt 进行密码的 hash
			.passwordEncoder(passwordEncoder());
	}

	@Bean
	@Override
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return super.authenticationManagerBean();
	}

	/**
	 * 装载BCrypt密码编码器
	 */
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

	/**
	 * 配置自定义jwt权限认证过滤器
	 */
	@Bean
	public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
		return new JwtAuthenticationTokenFilter();
	}

	/**
	 * 设置需要授权认证的资源,不需要权认能访问的资源
	 */
	@Override
	protected void configure(HttpSecurity httpSecurity) throws Exception {
		httpSecurity
			// we don't need CSRF because our token is invulnerable
			.csrf().disable()
			.cors().and() // 跨域
			.exceptionHandling().authenticationEntryPoint(myAuthenticationEntry).and()
			.exceptionHandling().accessDeniedHandler(myAccessDenied).and()
			// don't create session
			.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
			.authorizeRequests()
			//过滤掉不需要权限的地址
			.antMatchers(HttpMethod.GET, "/").permitAll()
			.antMatchers("/sso/login").permitAll()
			// 其它接口都要认证
			.anyRequest().authenticated();

		//将token验证添加在密码验证前面
		httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

		// disable page caching
		httpSecurity.headers().cacheControl();
	}
}
